package com.example.demo;
import com.example.demo.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import com.mongodb.client.result.DeleteResult;
import org.springframework.data.mongodb.core.query.*;

@Controller
public class WebController{ 
	
	@Autowired
  MongoTemplate mongoTemplate;
	
	@ResponseBody
	@GetMapping("/hello")
   public String hello() {
	 return "hello";
	
} 
	@ResponseBody
	@GetMapping("/Insert")
	public String Insert() {  //增加
		Person person =Person.builder().name("Ellis").age(33).id(2L).build();
		mongoTemplate.insert(person);
		return "Insert finish!";
	}
	
	@ResponseBody
	@GetMapping("/remove")
	public String Remove() {  //删除
		
        Query query = new Query(Criteria.where("id").is(1L));//通过查询id删除信息
        mongoTemplate.remove(query, Person.class);
		return "remove finish!";
	}
     
	
	@ResponseBody
	@GetMapping("/find")
	public String Find() {  //查询
		
        Query query = new Query(Criteria.where("id").is(2L));//通过id查询信息
       // mongoTemplate.remove(query, Person.class);
        Person result = mongoTemplate.findOne(query, Person.class);
        System.out.println("查询结果：" + result.toString());
		return "find finish!";
	}
	
	@ResponseBody
	@GetMapping("/updata")
	public String updata() {  //修改

	        Query query = new Query(Criteria.where("id").is(2L));
	        // 修改内容
	        Update update = new Update().set("name","宋江");
	        mongoTemplate.updateFirst(query,update,Person.class);
	        return "updata finish!";

	}
     
	



}


